home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu086.dms / pu086.adf / clibs / cstartup.c < prev    next >
C/C++ Source or Header  |  1990-12-04  |  5KB  |  200 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.   Cstartup.c    within        Public Domain c.lib
  11.  
  12.  
  13.   The high level startup code for all C programs.
  14. */
  15.  
  16. /* This file splits the command arguments into seperate strings and
  17.   passes an array of strings to the main routine. */
  18.  
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <workbench/startup.h>
  23.  
  24. extern APTR _stdin;        /* Standard file handles */
  25. extern APTR _stdout;
  26. extern long _cmndlen;
  27. extern char *_cmndstr;
  28. extern long _cmndnlen;
  29. extern char *_cmndnam;
  30. extern long _fromWB;
  31. extern struct WBStartup *_WBmsg;
  32.  
  33. extern int stdoutUnbuffered;
  34.  
  35. char cmndname[16] = "";    /* How do we get the name of this command? */
  36. char *WBargv[3] = {cmndname,NULL,NULL};
  37.  
  38. FILE *_iob[_NFILE];
  39.  
  40. _main()
  41.    {/* The initial entry point, called once cmndlen and cmndstr have
  42.        been set up */
  43.     register int  argc;
  44.     char **argv;
  45.     register int  count,space,arg_num;
  46.     char *cmnd_cpy;
  47.     char ch;
  48.     register FILE *f_blocks;
  49.  
  50.     f_blocks = (FILE *)calloc(sizeof(FILE),2);
  51.     if(!f_blocks)
  52.         _libc_error(MALLOC_ZERO);
  53.  
  54.     /* First set up the file handles */
  55.     f_blocks->_file = _stdin;
  56.     f_blocks->_flag = _IOREAD | _IONBF;
  57.     _iob[0] = f_blocks++;
  58.  
  59.     f_blocks->_file = _stdout;
  60.     if(stdoutUnbuffered)
  61.         f_blocks->_flag = _IOWRT | _IONBF;
  62.       else
  63.        {f_blocks->_flag = _IOWRT | _IOLBF;
  64.         f_blocks->_base = (unsigned char *)malloc(256);
  65.         if(f_blocks->_base == 0)
  66.             _libc_error(MALLOC_ZERO);
  67.     f_blocks->_bsiz = 256;
  68.         }
  69.     /* Make stderr and stdout the same file pointer */
  70.     _iob[1] = f_blocks;
  71.     _iob[2] = f_blocks;
  72.  
  73.     /* Now split the command string into arguments */
  74.     if(_fromWB)
  75.        {/* called from workbench, we just grab the file name argument
  76.           if we were called as a project */
  77.         struct WBArg *args;
  78.         char *arg1;
  79.  
  80.         argv = (char **)calloc(sizeof(char *),_WBmsg->sm_NumArgs);
  81.         if(argv == 0)
  82.             _libc_error(MALLOC_ZERO);
  83.         args = _WBmsg->sm_ArgList;
  84.         for(argc=0;argc < _WBmsg->sm_NumArgs;argc++)
  85.            {
  86.             argv[argc] = args->wa_Name;
  87.             CurrentDir(args->wa_Lock);
  88.             args++;
  89.             }
  90.         main(argc,argv);
  91.         }
  92.       else if(_cmndlen<=1 || _cmndstr==0)
  93.         /* Called with no arguments */
  94.         main(1,WBargv);
  95.       else
  96.        {/* We must split the buffer into arguments */
  97.  
  98.         /* Copy the command string to where we can fiddle with it */
  99.     cmnd_cpy = malloc((int)(_cmndlen+1));
  100.         strncpy(cmnd_cpy,_cmndstr,(int)_cmndlen);
  101.         cmnd_cpy[_cmndlen]='\0';
  102.  
  103.         /* Work out the number of arguments */
  104.         argc = 1;
  105.         space = 1;
  106.         for(count=0;count<_cmndlen;count++)
  107.            {register char this;
  108.         this = cmnd_cpy[count];
  109.         if(this=='\"')
  110.            {/* Quoted strings are a single argument */
  111.                 cmnd_cpy[count]='\0';
  112.         count++;
  113.         while(cmnd_cpy[count]!='\"' && cmnd_cpy[count]!='\0')
  114.             count++;
  115.                 cmnd_cpy[count]='\0';
  116.         argc++;
  117.         space=1;
  118.         }
  119.           else if(this==' '  || this=='\t' || this=='\0' || 
  120.                       this=='\n' || this=='\r')
  121.                {if (!space)
  122.                     space = 1;
  123.                 cmnd_cpy[count] = '\0';
  124.                 }
  125.               else
  126.                {if (space)
  127.                    {space = 0;
  128.                     argc++;
  129.                     }
  130.                 }
  131.             }
  132.  
  133.         /* argc now counts the number of arguments we have */
  134.         argv = (char **)calloc(argc+1,sizeof(char *));
  135.  
  136.         space   = 1;
  137.         arg_num = 1;
  138.         argv[0] = cmndname;
  139.         for(count=0;count<_cmndlen;count++)
  140.            {if(cmnd_cpy[count]=='\0')
  141.                {if (!space)
  142.                     space = 1;
  143.                 }
  144.               else
  145.                {if (space)
  146.                    {space = 0;
  147.                     argv[arg_num++] = &cmnd_cpy[count];
  148.                     }
  149.                 }
  150.             }
  151.         argv[arg_num] = NULL;
  152.         main(argc,argv);
  153.         }
  154.     exit(0);
  155.     }
  156.  
  157. typedef void (*voidfun)();
  158.  
  159. #define MAX_EXIT 32
  160.  
  161. static voidfun exit_funs[MAX_EXIT];
  162. static int exit_fun_count = 0;
  163.  
  164. exit(code)
  165.     int code;
  166.    {/* Quit from the program */
  167.     register int count;
  168.  
  169.     /* Call the exit functions */
  170.     for(count=exit_fun_count-1;count>=0;count--)
  171.         (*(exit_funs[count]))();
  172.  
  173.     /* Shut any open files, stdin & stdout are shut by _exit */
  174.     for(count=3;count<_NFILE;count++)
  175.        {/* Close any open files */
  176.         if(_iob[count] && _iob[count]->_file)
  177.             fclose(_iob[count]);
  178.         }
  179.     if(stderr->_file != stdout->_file)
  180.         fclose(stderr);
  181.  
  182.     fflush(stdout);
  183.     _exit((long)code);
  184.     }
  185.  
  186. int
  187. atexit(func)
  188.     voidfun func;
  189.    {/* If there is still space register the function */
  190.     if(exit_fun_count<MAX_EXIT)
  191.        {exit_funs[exit_fun_count] = func;
  192.         exit_fun_count++;
  193.         return(-1);
  194.         }
  195.       else
  196.         /* Ran out of function slots */
  197.         return(0);
  198.     }
  199.  
  200.